<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <title>酷数 - 登录验证</title>
</head>
<body>
<div id="app">
    <h3 v-text="msg"></h3>
</div>

<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="/static/js/vue.js"></script>
<script src="/static/js/axios.min.js"></script>

<!-- 解决IE兼容性 -->
<script src="/static/js/url-search-params-polyfill.js"></script>
<script src="/static/js/browser-polyfill.js"></script>
<script>
    if (!window.Promise) {
        window.Promise = Promise;
    }
</script>
<script>
    // 以分钟为单位，设置localStorage过期时间，用于存储服务器数据
    // set参数expire可不传，setItem到期时间标识expire会标识为-1,get时无过期时间，直接返回set中的val
    var localStorageUtil = {
        set: function (key, val, expire) {
            let exp = expire ? Date.now() + expire * 1000 * 60 : -1;
            localStorage.setItem(key, JSON.stringify({value: val, expire: exp}));
            console.log('set ' + key + ':' + JSON.stringify({value: val, expire: new Date(exp).toLocaleString()}));
        },
        get: function (key) {
            let data = localStorage.getItem(key);
            console.log(data);
            if (!data) return null;
            let dataObj = JSON.parse(data);
            if (dataObj.expire === -1) return dataObj.value;
            if (Date.now() >= dataObj.expire) {
                localStorage.removeItem(key);
                return null;
            } else {
                return dataObj.value;
            }
        }
    };

    new Vue({
        el: "#app",
        data: {
            code: "",
            msg: "正在验证登录，请稍后..."
        },
        mounted: function () {
            this.code = this.getUrlParam("code");
            this.jwtVerify();
        },
        methods: {
            getUrlParam: function (variable) {
                var query = window.location.href;
                var vars = query.split("?")[1].split("&");
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] === variable) {
                        return pair[1];
                    }
                }
                return "";
            },
            jwtVerify: function () {
                let _this = this;
                axios.get("/user/jwt_verify/", {params: {code: this.code}}).then(function (response) {
                    let res = response.data;
                    if (res.status === "success") {
                        _this.token = res.token;
                        // window.localStorage.setItem("token", res.token);
                        localStorageUtil.set("token", res.token, 60);
                        _this.msg = "验证成功，正在跳转，请稍后...";
                        window.location.href = "/?timestamp=" + new Date().getTime() + Math.random();
                    }
                })
            }
        }
    })
</script>
</body>
</html>
